// key  键名
// val  键值
// day  过期时间
// path  存储路径
function setCookie (key, val, day, path = "/") {
  // var val = "a123123";

  if (day) {   // 有过期时间
    var date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = key + "=" + val + ";expires=" + date.toUTCString() + ";path=" + path;
  } else {
    document.cookie = key + "=" + val + ";path=" + path;
  }
}

function getCookie (key) {
  var cookie = document.cookie;
  // console.log(cookie);
  if (cookie) {  //有cookie时
    var list = cookie.split("; ");
    // console.log(list); //  ["user=a123123", "pwd=123123", "phone=17386141517", "email=1272071495@qq.com"]
    for (var i = 0; i < list.length; i++) {
      var item = list[i]; // "user=a123123", "pwd=123123",
      var name = item.split("=")[0];
      var val = item.split("=")[1];
      if (name === key) {
        return val;
      }

    }
  }
  return "";
}

function delCookie (key) {
  setCookie(key, "", -1);
}


function clearCookie () {
  var cookie = document.cookie;
  // console.log(cookie);
  if (cookie) {  //有cookie时
    var list = cookie.split("; ");
    // console.log(list); //  ["user=a123123", "pwd=123123", "phone=17386141517", "email=1272071495@qq.com"]
    for (var i = 0; i < list.length; i++) {
      var item = list[i]; // "user=a123123", "pwd=123123",
      var key = item.split("=")[0];
      delCookie(key)
    }
  }
  return "";
}

